package com.yss.sofa.report.engine;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

//注意要将文件保存为utf-8，txt文件的默认保存为ANSI.要改成urf-8不然会像file:ziptestfile/fileInZip.txt
public class ReadZip {
	public static void main(String[] args) throws IOException {
		ReadZip rz = new ReadZip();
		File file = new File("X:\\repository");
		List<File> list = new ArrayList<File>();
		rz.getJarFile(file, list);
		for (File temp : list) {
			rz.readZipContext(temp.getAbsolutePath());
		}
	}

	public void getJarFile(File file, List<File> list) {
		if (file.isDirectory()) {
			for (File temp : file.listFiles()) {
				getJarFile(temp, list);
			}
		} else if (file.getName().endsWith("jar")) {
			list.add(file);
		}
	}

	public void readZipContext(String zipPath) throws IOException {
		ZipFile zf = null;
		try {
			zf = new ZipFile(zipPath);
		} catch (ZipException e) {
			System.out.println(zipPath);
			return;
		}

		InputStream in = new BufferedInputStream(new FileInputStream(zipPath));
		ZipInputStream zin = new ZipInputStream(in);
		ZipEntry ze;
		while ((ze = zin.getNextEntry()) != null) {
			if (ze.isDirectory()) {

			} else {
				BufferedReader reader;
				try {
					zf.getInputStream(ze).read(new byte[1000], 0, 1000);
				} catch (IOException e) {
					zin.close();
					in.close();
					zf.close();
					System.out.println(zipPath);
					File file = new File(zipPath);
					File fileParent = file.getParentFile();
					File[] childrenFiles = fileParent.listFiles();
					for (File temp : childrenFiles) {
						if (temp.delete()) {
							System.out.println("delete:" + temp.getAbsolutePath());
						}
					}
					break;
				}
			}

		}
	}

}
